package com.open.free.generator.utils;



import com.open.free.generator.vo.SqlArgsVo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/18 10:01
 * @Description
 */
public class FileIOUtils {
    /**
     * 文件夹生成
     * @param targetPath
     * @throws IOException
     */
    public static void getFileExists(String targetPath) throws IOException {
        //判断父目录是否存在
        File diskFile = new File(targetPath);
        if (!diskFile.exists()) {
            FileUtils.forceMkdir(diskFile);
        }
    }

    public static String diskConvert(String targetPath){
        return StringUtils.replace(targetPath,".",File.separator);
    }


    public static void createFile(String templateFile,String diskFilePath,String fileName) throws IOException {
        getFileExists(diskFilePath);
        InputStream inputStream = IOUtils.toInputStream(templateFile,"UTF-8");
        FileOutputStream fileOutputStream = new FileOutputStream(new File(diskFilePath+File.separator+fileName));
        IOUtils.copy(inputStream,fileOutputStream);
        inputStream.close();
        fileOutputStream.close();
    }

    public static void copyFileToPath(File file,String targetPath) throws IOException {
        FileUtils.copyFileToDirectory(file,new File(targetPath));
    }

    public static String getPackagePath(String packagePath){
        String[] packageArrays = StringUtils.split(packagePath,".");
        StringBuilder stringBuilder = new StringBuilder();
        for(String str: packageArrays){
            stringBuilder.append(str).append(File.separator);
        }
        return stringBuilder.toString();
    }
    public static String dealName(String columnName,String chart) {

        if (columnName.contains(chart)) {
            String[] column_name = columnName.split(chart);
            StringBuffer names = new StringBuffer();
            names.append(column_name[0]);

            for (int i = 1; i < column_name.length; i++) {
                String suffixStr = column_name[i].substring(0, 1).toUpperCase()
                        + column_name[i].substring(1, column_name[i].length());

                names.append(suffixStr);
            }
            columnName = names.toString();
        }

        return columnName;
    }
    public static String toUpperCaseFirst(String columnName) {
        return columnName.substring(0, 1).toUpperCase() + columnName.substring(1, columnName.length());
    }

    public static String toLowerCaseFirst(String columnName) {
        return columnName.substring(0, 1).toLowerCase() + columnName.substring(1, columnName.length());
    }
    /**
     * 驼峰首字母大写
     * @param columnName
     * @return
     */
    public static String beanName(String columnName){
        return toUpperCaseFirst(dealName(columnName,"_"));
    }

    /**
     * 驼峰首字母大写
     * @param columnName
     * @return
     */
    public static String beanNameVar(String columnName){
        return toLowerCaseFirst(dealName(columnName,"_"));
    }
    /**
     * 类名
     * @param tableName
     * @return
     */
    public static String clazzName(String tableName){
        if(tableName.startsWith("t_")){
            return beanName(StringUtils.substring(tableName,1));
        }else{
            return beanName(tableName);
        }
    }
    /**
     * 类的变量名
     * @param tableName
     * @return
     */
    public static String clazzVarName(String tableName){
        if(tableName.startsWith("t_")){
            return beanNameVar(StringUtils.substring(tableName,1));
        }else{
            return beanNameVar(tableName);
        }
    }

    public static List<SqlArgsVo> getQuestionResolution(String regex, String html){
        List<SqlArgsVo> argsList = new ArrayList<>();
        Matcher matcher = Pattern.compile(regex).matcher(html);
        while (matcher.find()){
            SqlArgsVo sqlArgsVo = new SqlArgsVo();
            String args = matcher.group().trim().replace("'#{","").replace("}'","");
            args = StringUtils.substringAfterLast(args,".");
            sqlArgsVo.setArgsKey(args);
            sqlArgsVo.setArgsValue("get"+toUpperCaseFirst(args)+"()");
            argsList.add(sqlArgsVo);
            System.out.println(args);
        }
        return argsList;
    }

}
